package com.mlz.mlzmall.ui.activity.act.main;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.google.gson.Gson;
import com.mlz.mlzmall.R;
import com.mlz.mlzmall.bean.CommentAllBean;
import com.mlz.mlzmall.bean.GoodsIntroduceBean;
import com.mlz.mlzmall.custom.ListScrollView;
import com.mlz.mlzmall.ui.activity.act.base.BaseActivity;
import com.mlz.mlzmall.ui.activity.act.order.ConfirmOrderActivity;
import com.mlz.mlzmall.ui.activity.act.personal.ServiceCenterActivity;
import com.mlz.mlzmall.ui.activity.act.personal.userLogin.LoginActivity;
import com.mlz.mlzmall.ui.activity.frag.main.CustomerEvaluationFragment;
import com.mlz.mlzmall.ui.activity.frag.main.GoodDetailsFragment;
import com.mlz.mlzmall.ui.dialog.AddCartPopupWindow;
import com.mlz.mlzmall.ui.dialog.CollectionPopupWindow;
import com.mlz.mlzmall.ui.dialog.ReuestDialog;
import com.mlz.mlzmall.ui.utils.mlz.ConfigInfo;
import com.mlz.mlzmall.ui.utils.mlz.URL;
import com.mlz.mlzmall.ui.utils.util.ACache;
import com.mlz.mlzmall.ui.utils.util.CacheUtils;
import com.mlz.mlzmall.ui.utils.util.LogUtils;
import com.mlz.mlzmall.ui.utils.util.SharedUtils;
import com.mlz.mlzmall.volley.volley.NetRequest;
import com.mlz.mlzmall.volley.volley.RequestListener;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by pm on 2016/5/20.
 */
public class GoogDetailsActivity extends BaseActivity {
    private String TAG = "GoogDetailsActivity";

    private RelativeLayout rl_back;
    private int productid;
    UpdateUIBroadcastReceiver broadcastReceiver;
    //商品详情上部数据
    private GoodsIntroduceBean.ProductInfoDto datas;
    private ListScrollView scrollView1;
    //图片
    private ViewPager viewPager;
    private List<ImageView> images = new ArrayList<ImageView>();
    private List<String> imagestrings;
    //fragment事务
    //图片切换的点
    private LinearLayout ly_images;
    private int oldePosition;
    private ArrayList<ImageView> docs = new ArrayList<ImageView>();
    //商品上部其他数据
    private TextView manufacturerName;
    private ImageView img_origin;
    private TextView tv_name;
    private TextView good_price;
    private TextView tv_price;
    //Framgent
    private LinearLayout rl_click1;
    private LinearLayout rl_click2;
    private TextView tv_user_evaluation;
    private ImageView img_choose1;
    private ImageView img_choose2;

    private FrameLayout frameLayout;
    //    private ViewPager viewPager2;
//    private ArrayList<Fragment> fragmentList;
    private GoodDetailsFragment goodDetailsFragment;
    private CustomerEvaluationFragment gustomerEvaluationFragment;
    //其它控件

    private ImageView img_cart;
    private ImageView img_serviceCenter;
    private ImageView img_up;
    //底部
    private Button bt_addProductToCart;
    private Button btn_goToPay;
    //收藏
    private ImageView img_add_collection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_details);
        initView();
        initListener();
        initData();
    }

    /**
     * 初始化View
     */
    private void initView() {
        rl_back = (RelativeLayout) findViewById(R.id.rl_title_topic);

        scrollView1 = (ListScrollView) findViewById(R.id.scrollView1);
        viewPager = (ViewPager) findViewById(R.id.banner);
        ly_images = (LinearLayout) findViewById(R.id.ly_pageview);

        manufacturerName = (TextView) findViewById(R.id.tv_goods_title);
        img_origin = (ImageView) findViewById(R.id.iv_goods_introduce_flag);
        tv_name = (TextView) findViewById(R.id.tv_name);
        good_price = (TextView) findViewById(R.id.tv_gods_introduce_prices);
        tv_price = (TextView) findViewById(R.id.tv_prices);

        rl_click1 = (LinearLayout) findViewById(R.id.rl_goods_details);
        rl_click2 = (LinearLayout) findViewById(R.id.rl_saysomething);
        tv_user_evaluation = (TextView) findViewById(R.id.tv_says_products);
        img_choose1 = (ImageView) findViewById(R.id.iv_goods1);
        img_choose2 = (ImageView) findViewById(R.id.iv_goods2);

        frameLayout = (FrameLayout) findViewById(R.id.id_content);
        //  viewPager2 = (ViewPager) findViewById(R.id.id_content);

        img_cart = (ImageView) findViewById(R.id.iv_cars);
        img_serviceCenter = (ImageView) findViewById(R.id.iv_customer_introduce_service);
        img_up = (ImageView) findViewById(R.id.iv_up_introduce);

        //底部
        bt_addProductToCart = (Button) findViewById(R.id.addProductShoppingDetailIntroduce);
        btn_goToPay = (Button) findViewById(R.id.payNow);
        img_add_collection = (ImageView) findViewById(R.id.iv_star_collection);
        IntentFilter filter = new IntentFilter();
        filter.addAction("action.updateUI");
        broadcastReceiver = new UpdateUIBroadcastReceiver();
        registerReceiver(broadcastReceiver, filter);

    }

    /**
     * 设置监听
     */
    private void initListener() {
        rl_back.setOnClickListener(this);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                docs.get(position).setBackgroundResource(R.drawable.dot_focused);
                docs.get(oldePosition).setBackgroundResource(R.drawable.dot_normal);
                oldePosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        rl_click1.setOnClickListener(this);
        rl_click2.setOnClickListener(this);

        img_cart.setOnClickListener(this);
        img_serviceCenter.setOnClickListener(this);
        img_up.setOnClickListener(this);

        img_add_collection.setOnClickListener(this);
        bt_addProductToCart.setOnClickListener(this);
        btn_goToPay.setOnClickListener(this);
    }

    /**
     * 加载数据
     */
    private void initData() {
        productid = getIntent().getIntExtra("productId", 0);
        //请求上部数据
        if (productid != 0) {

            getDataFromInfo(productid);
            //获取默认条数的全部评论
            getCustomerComments();
        }

        //  setDefaultFragment();
    }

    /**
     * 设置默认的Fragment
     */
    private void setDefaultFragment(String url) {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        goodDetailsFragment = GoodDetailsFragment.newInstance(url);
        transaction.replace(R.id.id_content, goodDetailsFragment);
        transaction.commit();
        img_choose1.setVisibility(View.VISIBLE);
        img_choose2.setVisibility(View.GONE);
    }

    /**
     * 获取商品图片
     *
     * @param urls
     */
    private void getImageFromUrl(List<String> urls) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(5, 3, 5, 3);
        for (int i = 0; urls.size() - i > 0; i++) {
            ImageView imageview = new ImageView(GoogDetailsActivity.this);
            ImageLoader.getInstance().displayImage(urls.get(i), imageview);
            images.add(imageview);

            ImageView view = new ImageView(this);
            view.setPadding(12, 12, 12, 12);
            view.setBackgroundResource(R.drawable.dot_normal);
            ly_images.addView(view, layoutParams);
            docs.add(view);
        }
        viewPager.setAdapter(new ViewPagerAdapter());
        docs.get(0).setBackgroundResource(R.drawable.dot_focused);
    }

    /**
     * 商品详细界面上部数据
     *
     * @param productid
     */
    private void getDataFromInfo(int productid) {
        ReuestDialog.showProgressDialog(GoogDetailsActivity.this);
        // JSONObject jsonObject = new JSONObject(getMall());
     //   JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, "http://app.mlzmall.com/api/product/info?productId=" + productid, null, new Response.Listener<JSONObject>() {
        JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, URL.PRODUCT.INFO+"?productId=" + productid, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                ReuestDialog.dissmissProgressDialog();
                LogUtils.d("商品详细界面上部数据：" + jsonObject.toString());
                Gson gson = new Gson();
                GoodsIntroduceBean bean = gson.fromJson(jsonObject.toString(), GoodsIntroduceBean.class);
                if (bean.getData() != null) {
                    LogUtils.d("商品详细界面上部数据："+bean.toString());
                    datas = bean.getData();
                    imagestrings = datas.getImages();
                    if (imagestrings != null && imagestrings.size() > 0) {
                        getImageFromUrl(imagestrings);
                    }
                    if (datas.getManufacturerName() != null && !(datas.getManufacturerName().equals(""))) {
                        manufacturerName.setText(datas.getManufacturerName());
                    }
                    if (datas.getAreaCode() != null && !(datas.getAreaCode().equals(""))) {
                        ImageLoader.getInstance().displayImage("http://m.mlzmall.com/Public/images/flags/" + datas.getAreaCode() + ".png", img_origin);
                    }
                    if (datas.getName() != null && !(datas.getName().equals(""))) {
                        tv_name.setText(datas.getName());
                    }
                    good_price.setText(datas.getPrice() + "");

                    String str_price = String.valueOf(datas.getRefPrice());
                    //判断是否是两位小数
                    if (str_price.split("\\.")[1].length()==2)
                    {
                        tv_price.setText(str_price);
                    }else
                    {
                        tv_price.setText(datas.getRefPrice() + "0");
                    }

                    setDefaultFragment(datas.getDetailHref());


                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("getDataFromInfo", error.getMessage(), error);

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                map.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + sessionId);
                map.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return map;
            }
        };

        // 设置该请求的标签
        jsonRequest.setTag("getDataFromInfo");
        // 将请求添加到队列中
        ((MyApplication) getApplication()).getRequestQueue().add(jsonRequest);
    }

    private void getCustomerComments() {
        ReuestDialog.showProgressDialog(GoogDetailsActivity.this);
        // JSONObject jsonObject = new JSONObject(getMall());
  //      JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, "http://app.mlzMall.com/api/product/review?productId=" + productid, null, new Response.Listener<JSONObject>() {
        JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, URL.PRODUCT.REVIEW+"?productId=" + productid, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                ReuestDialog.dissmissProgressDialog();
                LogUtils.d("用户评价：" + jsonObject.toString());
                if (jsonObject.toString() != null && !(jsonObject.toString().equals(""))) {
                    Gson gson = new Gson();
                    CommentAllBean commentAllBean = gson.fromJson(jsonObject.toString(), CommentAllBean.class);
                    List<CommentAllBean.CommentAll.Commentall> commentallList;
                    commentallList = commentAllBean.getData().getList();  //全部评论的各种数据
                    if (commentallList != null && commentallList.size() > 0) {
                        tv_user_evaluation.setText("用户评价(" + commentallList.size() + ")");
                    } else {
                        tv_user_evaluation.setText("用户评价(0)");
                    }
                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("getCustomerComments", error.getMessage(), error);

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                map.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + sessionId);
                map.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return map;
            }
        };

        // 设置该请求的标签
        jsonRequest.setTag("getCustomerComments");
        // 将请求添加到队列中
        ((MyApplication) getApplication()).getRequestQueue().add(jsonRequest);
    }

    @Override
    public void onClick(View v) {
        FragmentManager fm = getFragmentManager();
        // 开启Fragment事务
        FragmentTransaction transaction = fm.beginTransaction();
        switch (v.getId()) {
            //后退
            case R.id.rl_title_topic:
//                if((getIntent().getStringExtra("SpecialFragment"))!=null&&(getIntent().getStringExtra("SpecialFragment").equals("SpecialFragment")))
//                {
//                    GoogDetailsActivity.this.finishFromChild(GoogDetailsActivity.this.getParent());
//                }else {
//                    finish();
//                }
                finish();
                GoogDetailsActivity.this.overridePendingTransition(R.anim.right_in,R.anim.right_out);
                break;
            case R.id.rl_goods_details:
                if (goodDetailsFragment == null) {
                    goodDetailsFragment = GoodDetailsFragment.newInstance(datas.getDetailHref());
                }
                transaction.replace(R.id.id_content, goodDetailsFragment);
                transaction.commit();
                img_choose1.setVisibility(View.VISIBLE);
                img_choose2.setVisibility(View.GONE);
                break;
            case R.id.rl_saysomething:
                if (gustomerEvaluationFragment == null) {
                    gustomerEvaluationFragment = CustomerEvaluationFragment.newInstance(datas.getProductId());

                }
                transaction.replace(R.id.id_content, gustomerEvaluationFragment);

                transaction.commit();
                img_choose1.setVisibility(View.GONE);
                img_choose2.setVisibility(View.VISIBLE);

                break;
            case R.id.iv_cars:
                Intent intent1 = new Intent(GoogDetailsActivity.this, MainActivity.class);
//                intent1.putExtra("ShoppingCartFragment", "ShoppingCartFragment");
//                //前面的都finish activity
//                finish();
//                Intent intent1 = new Intent();
//                intent1.setAction("ShoppingCartFragment");
                sendBroadcast(new Intent("ShoppingCartFragment"));
                startActivity(intent1);
                break;
            case R.id.iv_customer_introduce_service:
                Intent intent = new Intent(GoogDetailsActivity.this, ServiceCenterActivity.class);
                startActivity(intent);
                break;
            case R.id.iv_up_introduce:
                scrollView1.scrollTo(0, 0);
                LogUtils.d("置顶");
                break;
            case R.id.addProductShoppingDetailIntroduce:
                if(!CacheUtils.getBoolean(GoogDetailsActivity.this, "Logined", false))
                {
                    getsessionKey();
                }else {
                    getDataToCart();
                }
                break;
            case R.id.payNow:
                //立即付款
                //判断是否登录
                if (!CacheUtils.getBoolean(GoogDetailsActivity.this, "Logined", false)) {
                    Intent intent3 = new Intent(GoogDetailsActivity.this, LoginActivity.class);
                    startActivity(intent3);
                } else {

                    getBuyNow();
                }
                break;
            case R.id.iv_star_collection:
                //收藏
                //判断是否登录和收藏
                if (!CacheUtils.getBoolean(GoogDetailsActivity.this, "Logined", false)) {
                    Intent intent2 = new Intent(GoogDetailsActivity.this, LoginActivity.class);
                    startActivity(intent2);
                } else {
                    setCollection();

                }
                break;
            default:
                break;
        }
    }

    private class ViewPagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return images.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(images.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(images.get(position));
            return images.get(position);
        }
    }

    /**
     * 定义广播接收器（内部类）
     *
     * @author lenovo
     */
    private class UpdateUIBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            tv_user_evaluation.setText("用户评价(" + String.valueOf(intent.getExtras().getInt("count")) + ")");
        }

    }

    @Override
    protected void onDestroy() {
        System.out.println("onDestroy");
        super.onDestroy();
        // 注销广播
        unregisterReceiver(broadcastReceiver);
    }

    public void getsessionKey() {
   //     NetRequest.get(this, "http://app.mlzMall.com/api/session/getSession", new RequestListener() {
        String str = ACache.get(GoogDetailsActivity.this).getAsString("sessionKeyLogin");
        if(str!=null&&str.length()>0)
        {
            return;
        }
        NetRequest.get(this, URL.SESSION.GET, new RequestListener() {
            @Override
            public void requestSuccess(String s) {
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(s);
                    String data = jsonObject.getString("data");
                    JSONObject json = new JSONObject(data);
                    String sessionKey = json.getString("sessionKey");
                    Log.i("GoogDetailsActivity", "返回的session成功" + sessionKey);
                    SharedUtils.clear(GoogDetailsActivity.this);
              //      SharedUtils.put(GoogDetailsActivity.this, ConfigInfo.SESSIONKEY, sessionKey);
               //     ACache.get(GoogDetailsActivity.this).put("sessionKeyAuth", sessionKey, ACache.TIME_DAY);
                    ACache.get(GoogDetailsActivity.this).put("sessionKeyLogin", sessionKey, ACache.TIME_DAY);
                    sessionId = ACache.get(GoogDetailsActivity.this).getAsString("sessionKeyLogin");
              //      Log.i("GoogDetailsActivity", "取session成功" + SharedUtils.get(GoogDetailsActivity.this, ConfigInfo.SESSIONKEY, ""));
                    getDataToCart();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void requestError(VolleyError e) {

            }
        });
    }

    /**
     * 添加商品到购物车
     */
    public void getDataToCart() {
        JSONObject jsonObject = new JSONObject(getMall());
    //    JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, "http://app.mlzMall.com/api/cart/add", jsonObject, new Response.Listener<JSONObject>() {
        JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, URL.CART.ADD, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                LogUtils.d("添加商品是否成功：" + jsonObject.toString());
                System.out.println("===json=ShoppingCartFragment!!!!" + jsonObject.toString());
                try {
                    String data = jsonObject.getString("data");
                    if (data != null && data.equals("success")) {
                        AddCartPopupWindow.AddCartPopup(GoogDetailsActivity.this);
                        //前面的都finish activity
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    System.out.println("===Broad=ShoppingCartFragment!!!!" + e.toString());
                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("getDataToCart", error.getMessage(), error);

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                map.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + sessionId);
                map.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return map;
            }
        };

        // 设置该请求的标签
        jsonRequest.setTag("getDataToCart");
        // 将请求添加到队列中
        ((MyApplication) getApplication()).getRequestQueue().add(jsonRequest);

    }

    /**
     * 添加购物车请求参数
     *
     * @return
     */
    private Map<String, String> getMall() {
        Map<String, String> param1 = new HashMap<String, String>();
        param1.put("productId", "" + productid);
        param1.put("quantity", "1");
        return param1;
    }

    /**
     * 添加判断收藏请求参数和添加收藏请求参数
     */
    private Map<String, String> getMall2() {
        Map<String, String> param1 = new HashMap<String, String>();
        param1.put("productId", "" + productid);
        return param1;
    }

    /**
     * 判断商品是否已收藏,是弹出已添加，不是就收藏
     */
    public void setCollection() {
        JSONObject jsonObject = new JSONObject(getMall2());
        LogUtils.d("ID" + productid);
        //  JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, URL.WISHLIST.ISIN, jsonObject, new Response.Listener<JSONObject>() {
     //   JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, "http://app.mlzMall.com/api/wishlist/isIn?productId=" + productid, null, new Response.Listener<JSONObject>() {
        JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, URL.WISHLIST.ISIN+"?productId=" + productid, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                LogUtils.d(jsonObject.toString());
                try {
                    String data = jsonObject.getString("data");
                    JSONObject json = new JSONObject(data);
                    Boolean inWish = json.getBoolean("inWish");
                    if (inWish) {
                        //已添加
                        CollectionPopupWindow.stateCollection(GoogDetailsActivity.this, "该商品已经收藏过了");
                    } else {
                        //去添加
                        addCollection();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("setCollection", error.getMessage(), error);

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                map.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + sessionId);
                LogUtils.d("sessionId" + sessionId);
                map.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return map;
            }
        };

        // 设置该请求的标签
        jsonRequest.setTag("setCollection");
        // 将请求添加到队列中
        ((MyApplication) getApplication()).getRequestQueue().add(jsonRequest);
    }

    /**
     * 添加收藏
     */
    public void addCollection() {
        JSONObject jsonObject = new JSONObject(getMall2());
   //     JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, "http://app.mlzMall.com/api/wishlist/add", jsonObject, new Response.Listener<JSONObject>() {
        JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, URL.WISHLIST.ADD, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                LogUtils.d("添加收藏:" + jsonObject.toString());
                try {
                    String msg = jsonObject.getString("msg");
                    if (msg != null && msg.equals("添加成功")) {
                        CollectionPopupWindow.stateCollection(GoogDetailsActivity.this, "该商品收藏成功");
                    } else {
                        CollectionPopupWindow.stateCollection(GoogDetailsActivity.this, "收藏失败，请重试");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("addCollection", error.getMessage(), error);

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                map.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + sessionId);
                map.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return map;
            }
        };

        // 设置该请求的标签
        jsonRequest.setTag("addCollection");
        // 将请求添加到队列中
        ((MyApplication) getApplication()).getRequestQueue().add(jsonRequest);
    }

    public void getBuyNow() {
        JSONObject jsonObject = new JSONObject(getMall());
    //    JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, "http://app.mlzMall.com/api/cart/buyNow", jsonObject, new Response.Listener<JSONObject>() {
        JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, URL.CART.BUY_NOW, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                LogUtils.d("立即购买" + jsonObject.toString());
                int status;
                try {
                    status = jsonObject.getInt("status");
                    if (status == 0) {
                        String data = jsonObject.getString("data");
                        JSONObject json = new JSONObject(data);
                        String warehouse = json.getString("warehouse");
                        Intent intent = new Intent(GoogDetailsActivity.this, ConfirmOrderActivity.class);
                        intent.putExtra("warehouse", warehouse);
                        startActivity(intent);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("gda_getBuyNow", error.getMessage(), error);

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                map.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + sessionId);
                map.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return map;
            }
        };

        // 设置该请求的标签
        jsonRequest.setTag("gda_getBuyNow");
        // 将请求添加到队列中
        ((MyApplication) getApplication()).getRequestQueue().add(jsonRequest);
    }

}
